﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace Remote_Server
{
    public partial class MainForm : Form
    {
        int port = 33012;
        public static TcpListener ServerSocket;
        public static List<Client> Clients;
        public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        public MainForm()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                Msg("Not implemented yet");
            }
            else
            {
                backgroundWorker1.RunWorkerAsync();
                Msg("Starting Server...");
                btnStart.Text = "Stop";
            }
        }

        private void Status_Click(object sender, EventArgs e)
        {
            Msg("Clients: " + Clients.Count);
        }



        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            IPEndPoint localEndPoint = MakeEndPoint();
            ServerSocket = new TcpListener(localEndPoint);
            
            Msg("Ip: " + localEndPoint.Address.ToString());
            Msg("Port: " + localEndPoint.Port.ToString());

            ServerSocket.Start();
            int counter = 0;
            Clients = new List<Client>();

            Msg("Server started!");

            while ((true))
            {
                counter += 1;

                TcpClient newSocket;
                newSocket = ServerSocket.AcceptTcpClient();

                Msg("Client " + counter.ToString() + " Started");

                Client newClient = new Client("Client " + counter.ToString(), counter, newSocket);
                newClient.Msg = new Client.MsgDelegate(this.Msg);
                Clients.Add(newClient);
                newClient.ClientThread.Start();
            }
        }


        private IPEndPoint MakeEndPoint()
        {
            string strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            //IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            //IPAddress ipAddress = ipHostInfo.AddressList[0];

            for (int i = 0; i < addr.Length - 1; i++)
            {
                if (addr[i].AddressFamily == AddressFamily.InterNetwork)
                    return new IPEndPoint(addr[i], port);
            }

            return new IPEndPoint(addr[addr.Length - 1], port);
        }

        delegate void MsgCallback(string text);
        public void Msg(string text)
        {
            if (txtLog.InvokeRequired)
                txtLog.Invoke(new MsgCallback(WriteMsg), text);

            else
                WriteMsg(text);
            
        }
        private void WriteMsg(string text)
        {
            txtLog.AppendText(text + Environment.NewLine);
        }
    }
}
